<?php
/** PAGE NOT ALLOWED
 * Verify if user called this page through a allowed page.
 * @message: That file cannot be displayed alone.
 */
  
// $allowed_page=1;
if (!isset($allowed_page)) { echo "That file cannot be displayed alone."; exit; }
//require_once("../config.php"); 
require_once("d5power.DAO.php");
require_once("Character.class.php");
	
//Dotk NPC class.
class Mail extends DAO {
	
	private $Mail_id;
	private $from;
	private $to;
	private $subject;
	private $text;
	

	

	
	//Getters and Setters
	public function getMail_id() {
		return $this->Mail_id;
	}
	
	public function setMail_id($Mail_id) {
		$this->Mail_id = $Mail_id;
	}
	
	public function getFrom() {
		return $this->from;
	}
	
	public function setFrom($from) {
		$this->from = $from;
	}
	
	public function getTo() {
		return $this->to;
	}
	
	public function setTo($to) {
		$this->to = $to;
	}
	
	public function getSubject() {
		return $this->subject;
	}
	
	public function setSubject($subject) {
		$this->subject = $subject;
	}
	
	public function getText() {
		return $this->text;
	}
	
	public function setText($text) {
		$this->text = $text;
	}
	
	
	function create($from,$to,$subject,$text) {
	
	
	try {	
	 
		$sql = "select character_id from CHARACTERS where NAME = ?";
		$result = $this->retrieve($sql, array($to));
		  
		  if ($result->isEmpty()) {
		   throw new Exception("The messenger could not find ".$to.". Are you sure this is the correct name?");
			exit;
		  }
		  else{
			$line = $result->getRow();
			$to_id = $line['character_id'];	  
			 $sql = "INSERT INTO MAIL (TO_ID, FROM_ID, TEXT, SUBJECT) VALUES(?,?,?,?)";
			$this->retrieve($sql, array($from,$to_id,$subject,$text));
				return true;
			}
	  }
		catch(Exception $e) {
		  return $e->getMessage();
		}
	}
	
	
	function delete($id) {
		$sql = "DELETE from MAIL WHERE MAIL_ID = ? ";
		$playerResult = $this->retrieve($sql,array($id));
	}	
	function load($id){
		$sql = "SELECT TO_ID, FROM_ID, TEXT, SUBJECT FROM MAIL WHERE MAIL_ID = ?";
		$playerResult = $this->retrieve($sql, array($id));
	
		if (!$playerResult->isEmpty()) {
		  $results = $playerResult->getRow();
		  $this->to = $results['to'];
		  $this->from = $results['from'];
		  $this->text = $results['text'];
		  $this->subject = $results['subject'];
		}
  	}
	
	public function listMailAsTable($character){	
	
		$query = "select * from Mail where TO_ID = ? order by Mail_id";
		$result = $this->retrieve($query, array($character));
		$chara= new Character();


        echo "<table>\n";
        echo "  <tbody>\n";
        echo "    <tr>\n";
        echo "      <td class=\"table_header\">\n";
        echo "        <p style=\"text-align: center\"><b>- - - - - - - - - - - - - - - </b></p>\n";
        echo "      </td>\n";
        echo "    </tr>\n";
        while($reg = $result->getRow()) {
		  $chara->load($reg['from_id']);		
          echo "    <tr>\n";
          echo "      <td>\n";
          echo "        <p>[Subject:]".$reg['subject']." by [Author:]".$chara->getName()."</p>\n";
          echo "      </td>\n";
		  echo "    </tr>\n";
          echo "      <td>\n";
          echo "        <p style=\"text-align: center;\">\n";
		  echo "        <p>".$reg['text']."\n";
		  echo "      </td>\n";
          echo "    </tr>\n";
		  echo "    <tr>\n";
          echo "      <td>\n";  
		  echo "        <p style=\"text-align: center;\">\n";
		  echo "          [<a href=\"mail.php?mail_id=".$reg['mail_id']."&action=delete\">Delete</a>] \n";        
          echo "        </p>\n";
          echo "      </td>\n";
          echo "    </tr>\n";
		  echo "    <tr>\n";
        echo "      <td class=\"table_header\">\n";
        echo "        <p style=\"text-align: center\"><b>- - - - - - - - - - - - - - - </b></p>\n";
        echo "      </td>\n";
        echo "    </tr>\n";
        }
        echo "  </tbody>\n";
        echo "</table>\n";	
  
  
  
  }
	
	
	
	
	
	

	
	
	

}
	
  //Test unit	
  	
 	
 //$c = new Mail();
  //$c->listMailAsTable("4");
  //$c->setNpc_id(1);
  //print($c->create("12","Jongas",'jaguara','SEUB'));
  //print($c->delete());
  
  ?>
